﻿
Imports System.Text
Imports System.Net.NetworkCredential
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic
Partial Class driver_emulate
    Inherits System.Web.UI.Page
    '  Dim connString As String = "Data Source=sql2008 ; Initial Catalog=service; User Id=Thunder48; Password=Thundertech169;"
    Dim driverInfo As driverInfo = New driverInfo()
    Dim conn As New SqlConnection(driverInfo.getConnectionString())
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ' Dim parent = CType(Me.Master, driver_MasterPage)
        If IsPostBack Then
            driverInformation()
        End If
        Try
            If IsPostBack = False Then

                Dim cmd As String
                Dim sql As SqlCommand = New SqlCommand
                Dim reader As SqlDataReader

                '  conn.ConnectionString = driverInfo.getConnectionString()
                If (conn.ConnectionString IsNot Nothing) Then
                    conn.Open()
                    cmd = "Select ID, last_name + ', ' + first_name AS namename from Driver ORDER BY last_name ASC"
                    sql.CommandText = cmd
                    sql.Connection = conn
                    reader = sql.ExecuteReader()
                    If reader.HasRows Then
                        driverList.DataSource = reader
                        driverList.DataValueField = "ID"
                        driverList.DataTextField = ("namename")
                        driverList.DataBind()
                    End If
                    conn.Close()
                End If
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try


    End Sub

    Protected Sub driverInformation()
        Dim cmd2 As String
        Dim activityid As String
        Dim sql2 As SqlCommand = New SqlCommand
        Dim classInfo As SqlDataReader
        ' conn.ConnectionString = driverInfo.getConnectionString
        If conn.ConnectionString IsNot Nothing Then
            conn.Open()
            Dim selection As String = driverList.SelectedValue
            cmd2 = "Select ID, first_name, last_name from Driver where ID =" & selection
            sql2.CommandText = cmd2
            sql2.Connection = conn
            Try
                classInfo = sql2.ExecuteReader()
                If classInfo.Read() Then
                    whichd.Text = "<b>Emulate</b> <br> Driver ID: " + classInfo(0).ToString() + " <br> Name: " + classInfo(1).ToString() + " " + classInfo(2).ToString() + " "
                End If
            Catch ex As Exception
                'Response.Write(ex)
            End Try
            conn.Close()
        End If
        

    End Sub
    Protected Sub emulateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles emulateButton.Click
        If driverList.SelectedIndex >= 0 Then
            EmulateDriver(driverList.SelectedItem.Value)
        Else
            Response.Write("Please Select a driver :)")
        End If


    End Sub
    Protected Sub EmulateDriver(ByVal driverid As Integer)

        Dim cmd3 As String

        Dim sql3 As SqlCommand = New SqlCommand

        Dim reader1 As SqlDataReader
        ' conn.ConnectionString = driverInfo.getConnectionString()
        If conn.ConnectionString IsNot Nothing Then
            conn.Open()
            cmd3 = "select id,password,first_name,last_name,email from Driver where ID='" & driverid & "'"

            sql3.CommandText = cmd3
            sql3.Connection = conn
            reader1 = sql3.ExecuteReader()

            If reader1.HasRows Then
                Try
                    reader1.Read()


                    Session("driver_id") = reader1.GetValue(0)
                    Session("first_name") = reader1.GetString(2)
                    Session("last_name") = reader1.GetString(3)
                    Session("email") = reader1.GetString(4)
                    Dim pw As String = reader1.GetString(1)
                    reader1.Close()
                    Session("semester") = getSemester()
                    Session("loggedin") = True


                    If pw = "cssa" Then
                        Response.Redirect("reset.aspx")
                    Else
                        If Session("referer") = Nothing Then
                            Response.Redirect("portal.aspx")
                        Else
                            Response.Redirect(Session("referer"))
                        End If

                    End If


                Catch
                    Response.Write(":( Something bad happened")
                End Try

            Else
                Response.Write(":( Something bad happened (Login not found?)")

            End If
            conn.Close()
        End If
        
    End Sub

    Public Function getSemester() As Integer

        Dim semester_yearid As Integer
        Dim cmd As String
        Dim sql As SqlCommand = New SqlCommand
        Dim reader As SqlDataReader
        'conn.ConnectionString = driverInfo.getConnectionString()
        If conn.ConnectionString IsNot Nothing Then
            'conn.Open()
            cmd = "select top (1)semester_yearid, semester, year, start_date, end_date from COMSERV_SEMESTER_YEAR where  (start_date < { fn NOW() }) order by semester_yearid desc"
            sql.CommandText = cmd
            sql.Connection = conn
            reader = sql.ExecuteReader()

            If reader.HasRows Then
                reader.Read()
                'semester_yearid = reader.GetString(0)
                semester_yearid = reader.GetValue(0)
                'semester_yearid = 12
            Else
                semester_yearid = 0
            End If
            conn.Close()
        End If
        'Return 11
        Return semester_yearid
    End Function
End Class
